package vip.liux.backend.infrastructure.textTemplate.spi;

import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.util.Locale;
import java.util.Map;

import static org.slf4j.LoggerFactory.getLogger;

public class ThymeleafTemplateRenderingEngine implements TemplateRenderingEngine {

    public static final String ENGINE_NAME = "thymeleaf";

    private static final Logger logger = getLogger(ThymeleafTemplateRenderingEngine.class);

    @Override
    public String render(String template, @Nullable Map<String, Object> model, @Nullable Locale locale, @Nullable Map<String, Object> globalContext) {
        return templateEngine.process(template, new Context(locale, model));
    }

    @Override
    public boolean supports(String renderEngine) {
        return ENGINE_NAME.equalsIgnoreCase(renderEngine);
    }

    private final TemplateEngine templateEngine;

    public ThymeleafTemplateRenderingEngine() {
        this.templateEngine = new TemplateEngine();
        logger.debug("ThymeleafTemplateRenderingEngine");
    }
}
